#include "staffresult.h"
#include "ui_staffresult.h"
#include "calendar.h"
#include <QKeyEvent>
#include "inquiresql.h"
#include <QDateTime>
#include <QMessageBox>

#include <QDesktopWidget>

StaffResult::StaffResult(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StaffResult)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    m_calendar = new Calendar;
    LineEdit = -1;
    LineEditEnable = true;

    QHeaderView* vheaderView = ui->tableWidgetStaffResult->verticalHeader();
    vheaderView->setHidden(true);

    //设置每行的大小
    ui->tableWidgetStaffResult->setColumnCount(9);
    ui->tableWidgetStaffResult->setColumnWidth(0,90);
    ui->tableWidgetStaffResult->setColumnWidth(1,90);
    ui->tableWidgetStaffResult->setColumnWidth(2,50);
    ui->tableWidgetStaffResult->setColumnWidth(3,114);
    ui->tableWidgetStaffResult->setColumnWidth(4,114);
    ui->tableWidgetStaffResult->setColumnWidth(5,114);
    ui->tableWidgetStaffResult->setColumnWidth(6,100);
    ui->tableWidgetStaffResult->setColumnWidth(7,100);
    ui->tableWidgetStaffResult->setColumnWidth(8,100);

    initLineEditTime();
    setQtableWidgetHeaderStyle();
    connect(m_calendar, SIGNAL(signalUpdateLineEditDate(QString)), this, SLOT(slotUpdateLineEditDate(QString)));
}

StaffResult::~StaffResult()
{
    delete ui;
}

bool StaffResult::eventFilter(QObject *target, QEvent *event)
{
    if ( target == ui->lineEditDateEnd || target == ui->lineEditDateStart )
    {
        //鼠标左键释放事件
        if (event->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                QString time;
                if (target == ui->lineEditDateEnd)
                {
                    LineEdit = end;
                    time = ui->lineEditDateEnd->text();
                }
                else if (target == ui->lineEditDateStart)
                {
                    LineEdit = start;
                    time = ui->lineEditDateStart->text();
                }
                if (LineEditEnable)
                {
                    m_calendar->move((QApplication::desktop()->width() - m_calendar->width())/2,
                                     (QApplication::desktop()->height() - m_calendar->height())/2);
                    m_calendar->setSelectedCurrentDate(time);
                    m_calendar->setWindowFlags(
                                Qt::WindowStaysOnTopHint | Qt::ToolTip | Qt::FramelessWindowHint);

                    if (LineEdit == start)
                    {
                        QPoint GlobalPoint(ui->lineEditDateStart->mapToGlobal(QPoint(0, 0)));
                        int x = GlobalPoint.x();
                        int y = GlobalPoint.y();
                        m_calendar->setGeometry(x , y + 35, 150, 130);
                    }
                    else if (LineEdit == end)
                    {
                        QPoint GlobalPoint(ui->lineEditDateEnd->mapToGlobal(QPoint(0, 0)));
                        int x = GlobalPoint.x();
                        int y = GlobalPoint.y();
                        m_calendar->setGeometry(x , y + 35, 150, 130);
                    }
                    //在对 QWidget 操作时，不能操作其他东西
                    m_calendar->setWindowModality(Qt::ApplicationModal);
                    m_calendar->setAttribute(Qt::WA_ShowModal, true);
                    m_calendar->show();
                }
            }
        }
    }
    return QWidget::eventFilter(target, event);
}

void StaffResult::displayTableWidgetStaffResult()
{
    m_list.clear();
    ui->tableWidgetStaffResult->clear();

    if (LineEditEnable)
        m_list = m_sql->InquireStaffResultList(ui->lineEditDateStart->text(),
                                             ui->lineEditDateEnd->text());
    else
        m_list = m_sql->InquireStaffResultList("all", "all");

    setQtableWidgetHeaderStyle();

        //显示员工业绩信息
    ui->tableWidgetStaffResult->setRowCount(m_list.count());

    for(int i = 0; i < m_list.count(); ++i)
    {
        QTableWidgetItem *staffID           = new QTableWidgetItem(
                    m_list.at(i).get_personID());
        QTableWidgetItem *staffName         = new QTableWidgetItem(
                    m_list.at(i).get_personName());
        QTableWidgetItem *gender            = new QTableWidgetItem(
                    m_list.at(i).get_personGender());
        QTableWidgetItem *rechargeTimes     = new QTableWidgetItem(
                    m_list.at(i).get_rechargeTimes());
        QTableWidgetItem *rechargeAmount    = new QTableWidgetItem(
                    m_list.at(i).get_rechargeAmount());
        QTableWidgetItem *saleQuantity      = new QTableWidgetItem(
                    m_list.at(i).get_saleQuantity());
        QTableWidgetItem *totalSales        = new QTableWidgetItem(
                    m_list.at(i).get_totalSales());
        QTableWidgetItem *cost              = new QTableWidgetItem(
                    m_list.at(i).get_cost());
        QTableWidgetItem *profit            = new QTableWidgetItem(
                    m_list.at(i).get_profit());

        //设置商品编号,单价 数量 合计 为居中对齐
        staffID->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        staffName->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        gender->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        rechargeTimes->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        rechargeAmount->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        saleQuantity->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        totalSales->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        cost->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        profit->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );

        //设置文字颜色
        rechargeTimes->setTextColor(QColor(0, 0, 255));
        saleQuantity->setTextColor(QColor(0, 0, 255));
        rechargeAmount->setTextColor(QColor(255, 0, 0));
        totalSales->setTextColor(QColor(255, 0, 0));
        profit->setTextColor(QColor(255, 0, 255));

        staffID->setFont(QFont("DejaVu Sans", 11, 2, false));
        staffName->setFont(QFont("DejaVu Sans", 11, 2, false));
        gender->setFont(QFont("DejaVu Sans", 11, 2, false));
        rechargeTimes->setFont(QFont("DejaVu Sans", 11, 2, false));
        rechargeAmount->setFont(QFont("DejaVu Sans", 11, 2, false));
        saleQuantity->setFont(QFont("DejaVu Sans", 11, 2, false));
        totalSales->setFont(QFont("DejaVu Sans", 11, 2, false));
        cost->setFont(QFont("DejaVu Sans", 11, 2, false));
        profit->setFont(QFont("DejaVu Sans", 11, 2, false));

        ui->tableWidgetStaffResult->setItem(i, 0, staffID);
        ui->tableWidgetStaffResult->setItem(i, 1, staffName);
        ui->tableWidgetStaffResult->setItem(i, 2, gender);
        ui->tableWidgetStaffResult->setItem(i, 3, rechargeTimes);
        ui->tableWidgetStaffResult->setItem(i, 4, rechargeAmount);
        ui->tableWidgetStaffResult->setItem(i, 5, saleQuantity);
        ui->tableWidgetStaffResult->setItem(i, 6, totalSales);
        ui->tableWidgetStaffResult->setItem(i, 7, cost);
        ui->tableWidgetStaffResult->setItem(i, 8, profit);
    }

    ui->tableWidgetStaffResult->setCurrentItem(
                ui->tableWidgetStaffResult->item(m_list.count() - 1, 0));

    setTableWidgetColor();
}

void StaffResult::setQtableWidgetHeaderStyle()
{
    QStringList headers;
    headers << tr("员工编号") << tr("员工姓名") << tr("性别") << tr("会员储值次数")
            << tr("会员储值总额") << tr("商品销售数量") << tr("销售总额")
            << tr("成本总计") << tr("毛利");

    ui->tableWidgetStaffResult->setColumnCount(9);
    ui->tableWidgetStaffResult->setHorizontalHeaderLabels(headers);

    for (int i = 0; i < 9; ++i)
    {
        ui->tableWidgetStaffResult->horizontalHeaderItem(i)->setFont(QFont("Helvetica", 12, 2, false));
        ui->tableWidgetStaffResult->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
    }
}

void StaffResult::setTableWidgetColor()
{
    for (int i = 0; i < ui->tableWidgetStaffResult->rowCount(); ++i)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < ui->tableWidgetStaffResult->columnCount(); ++j)
            {
                QTableWidgetItem *item = ui->tableWidgetStaffResult->item(i,j);
                if (item)
                {
                    const QColor color = QColor(252,222,156);
                    item->setBackgroundColor(color);
                }
            }
        }
    }
}

void StaffResult::initLineEditTime()
{
    ui->lineEditDateStart->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
    ui->lineEditDateEnd->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
    ui->tableWidgetStaffResult->clear();
    ui->tableWidgetStaffResult->setRowCount(0);
    setQtableWidgetHeaderStyle();
}

void StaffResult::slotUpdateLineEditDate(QString date)
{
    if (LineEdit == start)
    {
        ui->lineEditDateStart->setText(date);
        return;
    }
    if (LineEdit == end)
    {
        ui->lineEditDateEnd->setText(date);
        return;
    }
    LineEdit = -1;
}

void StaffResult::on_pushButtonInquire_clicked()
{
    int startYear = ui->lineEditDateStart->text().mid(0, 4).toInt();
    int startMonth = ui->lineEditDateStart->text().mid(5, 2).toInt();
    int startDay = ui->lineEditDateStart->text().mid(8, 2).toInt();

    int endYear = ui->lineEditDateEnd->text().mid(0, 4).toInt();
    int endMonth = ui->lineEditDateEnd->text().mid(5, 2).toInt();
    int endDay = ui->lineEditDateEnd->text().mid(8, 2).toInt();

    if ( (startYear > endYear) || (startYear == endYear && startMonth > endMonth)
         || (startYear == endYear && startMonth == endMonth && startDay > endDay) )
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                 tr("\n开始日期大于结束日期，请重新设置") );
    }
    else
    {
        displayTableWidgetStaffResult();
    }
}

void StaffResult::on_comboBox_activated(int index)
{
    QDateTime currentTime = QDateTime::currentDateTime();//获取系统现在的时间
//    QDateTime startTime, endTime;
    QString startTimeString, endTimeString;
    switch (index)
    {
        case 0 :  //今天
            startTimeString = currentTime.toString("yyyy-MM-dd");
            endTimeString = startTimeString;
            break;
        case 1 :  //昨天
            startTimeString = currentTime.addDays(-1).toString("yyyy-MM-dd");
            endTimeString = startTimeString;
            break;
        case 2 :  //本月
            startTimeString = currentTime.toString("yyyy-MM-dd").replace(8, 2, "01");
            endTimeString = currentTime.toString("yyyy-MM-dd");
            break;
        case 3 :  //上月
            startTimeString = currentTime.addMonths(-1).toString("yyyy-MM-dd").replace(8, 2, "01");
            endTimeString = currentTime.addDays(currentTime.toString("d").toInt() * -1)
                    .toString("yyyy-MM-dd");
            break;
        case 4 :  //今年
            startTimeString = currentTime.toString("yyyy-MM-dd").replace(5, 5, "01-01");
            endTimeString = currentTime.toString("yyyy-MM-dd");
            break;
        case 5 :  //去年
            startTimeString = currentTime.addYears(-1).toString("yyyy-MM-dd").replace(5, 5, "01-01");
            endTimeString = currentTime.addYears(-1).toString("yyyy-MM-dd").replace(5, 5, "12-31");
            break;
        case 6 :  //全部
            ui->lineEditDateStart->setText(tr("全部"));
            ui->lineEditDateEnd->setText(tr("全部"));
            ui->lineEditDateStart->setDisabled(true);
            ui->lineEditDateEnd->setDisabled(true);
            LineEditEnable = false;
            return;
        default :
            break;
    }
    ui->lineEditDateStart->setDisabled(false);
    ui->lineEditDateEnd->setDisabled(false);
    LineEditEnable = true;
    ui->lineEditDateStart->setText(startTimeString);
    ui->lineEditDateEnd->setText(endTimeString);
}
